/**
 * 测试API密钥配置和使用
 */
public class APIKeyTest {
    
    public static void main(String[] args) {
        System.out.println("=== API密钥配置测试 ===\\n");
        
        // 创建用户偏好设置实例
        UserPreferences preferences = new UserPreferences();
        
        // 测试设置一个真实的WeatherAPI.com密钥
        String testWeatherApiKey = "d4a383b9c51e42b1848172246252406"; // 从配置文件中读取的密钥
        preferences.setWeatherApiComKey(testWeatherApiKey);
        
        // 验证密钥是否正确保存和读取
        String retrievedKey = preferences.getWeatherApiComKey();
        System.out.println("设置的密钥: " + testWeatherApiKey);
        System.out.println("读取的密钥: " + retrievedKey);
        System.out.println("密钥匹配: " + testWeatherApiKey.equals(retrievedKey));
        System.out.println("WeatherAPI.com密钥有效: " + preferences.hasValidWeatherApiComKey());
        System.out.println();
        
        // 测试WeatherAPI.com服务
        System.out.println("测试WeatherAPI.com服务:");
        WeatherAPIComService weatherApiService = new WeatherAPIComService(preferences);
        System.out.println("服务可用性: " + weatherApiService.isServiceAvailable());
        System.out.println("API状态: " + weatherApiService.getAPIStatus());
        
        if (weatherApiService.isServiceAvailable()) {
            System.out.println("连接测试: " + weatherApiService.testConnection() + "ms");
            
            // 测试获取天气数据
            try {
                Location testLocation = new Location("Beijing");
                WeatherData weather = weatherApiService.getCurrentWeather(testLocation);
                if (weather != null) {
                    System.out.println("✓ 成功获取北京天气数据:");
                    System.out.println("  温度: " + weather.getTemperature() + "°C");
                    System.out.println("  湿度: " + weather.getHumidity() + "%");
                    System.out.println("  描述: " + weather.getDescription());
                } else {
                    System.out.println("✗ 获取天气数据失败");
                }
            } catch (Exception e) {
                System.out.println("✗ 天气数据获取异常: " + e.getMessage());
            }
        } else {
            System.out.println("⚠ WeatherAPI.com服务不可用，可能的原因:");
            System.out.println("  - API密钥无效或未配置");
            System.out.println("  - 网络连接问题");
            System.out.println("  - API服务器故障");
        }
        
        System.out.println("\\n测试完成！");
    }
} 